﻿$(function () {
    $("#create").click(function () {

        $("#TypeID").val("0");
        $("#TypeName").val("");
        hideModal($("#editcatbox"));
        showModal($("#edittypebox"));
    });

    $("#typecancel").click(function () {
        hideModal($("#edittypebox"));
    });

    $("#typesubmit").click(function () {
        SubmitType();
    });

    $("#catcancel").click(function () {
        hideModal($("#editcatbox"));
    });

    $("#catsubmit").click(function () {
        SubmitCategory();
    });

    $(".typeadd").click(fTypeAdd);
    $(".typeedit").click(fTypeEdit);
    $(".typedelete").click(fTypeDelete);
    $(".showhide").click(fShowHide);

    $(".catedit").click(fCatEdit);
    $(".catdelete").click(fCatDelete);
});

function fTypeAdd() {

    var typeid = $(this).parent().attr("id").split("_")[1];
    $("#CategoryID").val("0");
    $("#CategoryTypeID").val(typeid);
    $("#CategoryName").val("");
    hideModal($("#edittypebox"));
    showModal($("#editcatbox"));
}

function fTypeEdit() {
    var typeid = $(this).parent().attr("id").split("_")[1];

    var input = {
        TypeID: typeid
    };

    Ajax({
        url: "/Businesses/GetType",
        data: JSON.stringify(input),
        success: function (data) {
            $("#TypeID").val(data.Type.ID);
            $("#TypeName").val(data.Type.Name);
            hideModal($("#editcatbox"));
            showModal($("#edittypebox"));
        }
    });
}

function fTypeDelete() {

    if (!confirm("are you sure?")) {
        return;
    }

    var typeid = $(this).parent().attr("id").split("_")[1];

    var input = {
        TypeID: typeid
    };

    Ajax({
        url: "/Businesses/DeleteType",
        data: JSON.stringify(input),
        success: function (data) {
            $("#t_" + input.TypeID).fadeOut(1000, function () {
                $(this).remove();
            });

            ShowMessage("deleted");
        }
    });
}

function fShowHide() {

    var typeid = $(this).parent().attr("id").split("_")[1];
    $("#tul_" + typeid).toggle();
    var src = $("#tul_" + typeid).is(":visible") ? "/Content/images/tree_minus.png" : "/Content/images/tree_plus.png";
    $("img", $(this)).attr("src", src);
}


function fCatEdit() {
    var catid = $(this).parent().attr("id").split("_")[1];

    var input = {
        CategoryID: catid
    };

    Ajax({
        url: "/Businesses/GetCategory",
        data: JSON.stringify(input),
        success: function (data) {
            $("#CategoryID").val(data.Category.ID);
            $("#CategoryTypeID").val(data.Category.Type.ID);
            $("#CategoryName").val(data.Category.Name);
            showModal($("#editcatbox"));
            hideModal($("#edittypebox"));
        }
    });
}

function fCatDelete() {

    if (!confirm("are you sure?")) {
        return;
    }

    var catid = $(this).parent().attr("id").split("_")[1];

    var input = {
        CategoryID: catid
    };

    Ajax({
        url: "/Businesses/DeleteCategory",
        data: JSON.stringify(input),
        success: function (data) {
            $("#c_" + input.CategoryID).fadeOut(1000, function () {
                $(this).remove();
            });
            ShowMessage("deleted");
        }
    });
}

function SubmitType() {

    $("#typesubmit").hide();

    var input = {
        ID: $("#TypeID").val(),
        Name: $("#TypeName").val()
    };

    Ajax({
        url: "/Businesses/SaveType",
        data: JSON.stringify(input),
        success: function (data) {
            ShowValidationErrors(data, { "Name": "TypeName" });
            if (data.IsSaved == true) {
                hideModal($("#edittypebox"));
                ShowMessage("saved");

                if (input.ID == "0") {
                    var html = "<li id=\"t_" + data.ID + "\" class=\"level1\">";
                    html += "<a href=\"#\" class=\"showhide\"><img src=\"/Content/images/tree_plus.png\" /></a>";
                    html += "<span>" + input.Name + "</span>";
                    html += "<a href=\"#\"class=\"typedelete\"><img src=\"/Content/images/cross.png\" alt=\"delete\" /></a>";
                    html += "<a href=\"#\" class=\"typeedit\"><img src=\"/Content/images/pencil.png\" alt=\"Edit\" /></a>";
                    html += "<a href=\"#\" class=\"typeadd\"><img src=\"/Content/images/add.png\" alt=\"Add\" /></a>";
                    html += "<ul id=\"tul_" + data.ID + "\"></ul></li>";

                    $("#types").append(html);

                    $("#t_" + data.ID + " .typeadd").click(fTypeAdd);
                    $("#t_" + data.ID + " .typeedit").click(fTypeEdit);
                    $("#t_" + data.ID + " .typedelete").click(fTypeDelete);
                    $("#t_" + data.ID + " .showhide").click(fShowHide);
                }

                $("#t_" + input.ID + " span:first").text(input.Name);
                $("#t_" + input.ID + " span:first").fadeOut(1000, function () {
                    $(this).fadeIn(2000)
                });
            }
        },
        complete: function () {
            $("#typesubmit").show();
        }
    });
}

function SubmitCategory() {

    $("#catsubmit").hide();

    var input = {
        ID: $("#CategoryID").val(),
        TypeID: $("#CategoryTypeID").val(),
        Name: $("#CategoryName").val()
    };

    Ajax({
        url: "/Businesses/SaveCategory",
        data: JSON.stringify(input),
        success: function (data) {
            ShowValidationErrors(data, { "Name": "CategoryName" });
            if (data.IsSaved == true) {
                hideModal($("#editcatbox"));
                ShowMessage("saved");

                if (input.ID == "0") {
                    var html = "<li id=\"c_" + data.ID + "\" class=\"level2\"><span>" + input.Name + "</span>";
                    html += "<a href=\"#\"class=\"catdelete\"><img src=\"/Content/images/cross.png\" alt=\"delete\" /></a>";
                    html += "<a href=\"#\" class=\"catedit\"><img src=\"/Content/images/pencil.png\" alt=\"Edit\" /></a>";
                    html += "</li>";

                    $("#tul_" + input.TypeID).append(html);

                    $("#c_" + data.ID + " .typeedit").click(fCatEdit);
                    $("#c_" + data.ID + " .typedelete").click(fCatDelete);
                }

                $("#c_" + input.ID + " span:first").text(input.Name);
                $("#c_" + input.ID + " span:first").fadeOut(1000, function () {
                    $(this).fadeIn(2000)
                });
            }
        },
        complete: function () {
            $("#catsubmit").show();
        }
    });
}